function checkPermision(name) {
  return new Promise((resolve, reject) => {
    var resultList = api.hasPermission({
      list: [name]
    })
// 权限已开
    if ( resultList[0].name === name && resultList[0].granted ) {
      resolve()
    } else {
// 权限未开,请求权限
      requestPermision(name).then(res => {
        resolve()
      }).catch(err => {
        reject({errMsg: '权限开启失败'})
      })
    }
  })

}

function requestPermision(name) {
  return new Promise((resolve, reject) => {
    api.requestPermission({
      list: [name],
      code: 1
    }, (ret, err) => {
// 请求权限成功
      if ( ret.list[0].name === name && ret.list[0].granted ) {
        resolve()
      } else {
// 请求权限失败
        reject()
      }
    })
  })
}

export {checkPermision}
